<?php
/*
*后台动作模块控制器
*/
class Action extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('action_model');
		$this->load->helper('form');
		$this->load->library('form_validation');
	}
	public function index(){
		$this->lists();
	}
	public function lists(){
	   $actions = $this->action_model->getTopAction();
	   
	   $a = array(''=>'请选择');
	   foreach($actions as $action){
			$a[$action['id']] = $action['action_info'];
	   }
		$data = array(
			'actions' => $a
		);
		
		
		$this->load->view('templates/header');
		$this->load->view('admin/action/list',$data);
		$this->load->view('templates/footer');
	}
	
	public function add(){
		
		
		$this->form_validation->set_rules('action_code','action_code','required');
		$this->form_validation->set_rules('action_info','action_info','required');
		
		//表单规则不符合
		if($this->form_validation->run() === false){
			
			$this->lists();
				
		}else{
			
			$data = array(
				'action_code' => trim($this->input->post('action_code'),'/'),
				'action_info' => $this->input->post('action_info'),
				'parent_id' => (int)$this->input->post('parent_id')
			);
		
			//不存在对应的action_code
			if(!$this->action_model->haveAction($data['action_code'])){
		
				//添加成功
				if($this->action_model->addAction($data)){
					$this->lists();
				}
			//已经存在对应的action_code	
			}else{
				$this->lists();
			}
			
		}
		
		
	}
}